package com.yc.taixin.config;

import com.yc.taixin.support.utils.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class ResourcesConfig extends WebMvcConfigurationSupport {

	private static final Logger log = LoggerFactory.getLogger(ResourcesConfig.class);

	/**
	 * 配置静态文件
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations(
				ResourceUtils.CLASSPATH_URL_PREFIX + "/static/", "file:static/");
		super.addResourceHandlers(registry);
	}

	/**
	 * 雪花算法
	 */
	@Bean
	public IdWorker idWorker() {
		return new IdWorker(0, 0);
	}

	/**
	 * 登录拦截器配置
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		log.info("**************开启登录拦截************");
		//注册TestInterceptor拦截器
		InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
		registration.addPathPatterns("/yuyue/**");                      //所有路径都被拦截
		registration.excludePathPatterns(                         //添加不拦截路径
				"/user/login", "/user/regist",     //登录注册
				"/**/*.html",            //html静态资源
				"/static/**",              //静态资源
				"/yuyue/pay",             //普通方法
				"/yuyue/options",           //公共页面
				"/yuyue/inletMain",           //公共页面
				"/yuyue/getYuyueConf",      //普通方法
				"/yuyue/saveYuyue"         //公共方法

		);
	}
}
